var moment = require('moment')
function getCurrentTime() {
  var keep = ''
  var date = new Date()
  var y = date.getFullYear()
  var m = date.getMonth() + 1
  m = m < 10 ? '0' + m : m
  var d = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  var f = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  keep = y + '' + m + '' + d + '' + h + '' + f + '' + s
  return keep // 20160614134947
}

function objLength(input) {
  var type = toString(input)
  var length = 0
  if (type !== '[object Object]') {
    // throw '输入必须为对象{}！'
  } else {
    for (var key in input) {
      if (key !== 'number') {
        length++
      }
    }
  }
  return length
}
// 验证是否是手机号码
function vailPhone(number) {
  let flag = false
  let myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/
  if (number.length !== 11) {
    flag = false
  } else if (!myreg.test(number)) {
    flag = false
  } else {
    flag = true
  }
  return flag
}
// 验证是否西班牙手机(6开头 9位数)
function ifSpanish(number) {
  let flag = false
  let myreg = /^([6|7|9]{1}(\d+){8})$/
  if (number.length !== 9) {
    flag = false
  } else if (!myreg.test(number)) {
    flag = false
  } else {
    flag = true
  }
  return flag
}
// 浮点型除法
function div(a, b) {
  var c = 0
  var d = 0
  var e = 0
  var f = 0
  try {
    e = a.toString().split('.')[1].length
  } catch (g) {}
  try {
    f = b.toString().split('.')[1].length
  } catch (g) {}
  c = Number(a.toString().replace('.', ''))
  d = Number(b.toString().replace('.', ''))
  return mul(c / d, Math.pow(10, f - e))
}
// 浮点型加法函数
function accAdd(arg1, arg2) {
  var r1, r2, m
  try {
    r1 = arg1.toString().split('.')[1].length
  } catch (e) {
    r1 = 0
  }
  try {
    r2 = arg2.toString().split('.')[1].length
  } catch (e) {
    r2 = 0
  }
  m = Math.pow(10, Math.max(r1, r2))
  return ((arg1 * m + arg2 * m) / m).toFixed(2)
}
//  浮点型乘法
function mul(a, b) {
  var c = 0
  var d = a.toString()
  var e = b.toString()
  try {
    c += d.split('.')[1].length
  } catch (f) {}
  try {
    c += e.split('.')[1].length
  } catch (f) {}
  return (
    (Number(d.replace('.', '')) * Number(e.replace('.', ''))) / Math.pow(10, c)
  )
}

//  遍历对象属性和值
function displayProp(obj) {
  var names = ''
  for (var name in obj) {
    names += name + obj[name]
  }
  return names
}
// 从对象数组中提取符合条件的数组
function findFieldEq(key, value, list) {
  var array = []
  for (var item in list) {
    var flag = false
    for (var fieldName in list[item]) {
      if (fieldName === key) {
        if (list[item][fieldName] === value) {
          flag = true
        }
      }
    }
    if (flag) {
      array.push(list[item])
    }
  }
  return array
}

//  转换星星分数
function convertStarArray(score) {
  //  1 全星,0 空星,2 半星
  var arr = []
  for (var i = 1; i <= 5; i++) {
    if (score >= i) {
      arr.push(1)
    } else if (score > i - 1 && score < i + 1) {
      arr.push(2)
    } else {
      arr.push(0)
    }
  }
  return arr
}

// 毫秒转分钟

function millisecondToDate(msd) {
  var time = parseFloat(msd) / 1000
  if (time !== null && time !== '') {
    if (time > 60 && time < 60 * 60) {
      time =
        parseInt(time / 60.0) +
        '分钟' +
        parseInt((parseFloat(time / 60.0) - parseInt(time / 60.0)) * 60) +
        '秒'
    } else if (time >= 60 * 60 && time < 60 * 60 * 24) {
      time =
        parseInt(time / 3600.0) +
        '小时' +
        parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) +
        '分钟' +
        parseInt(
          (parseFloat(
            (parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60
          ) -
            parseInt(
              (parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60
            )) *
            60
        ) +
        '秒'
    } else {
      time = parseInt(time) + '秒'
    }
  } else {
    time = '0时0分0秒'
  }
  return time
}
// 去除数组空值

function trimSpace(array) {
  for (var i = 0; i < array.length; i++) {
    if (array[i] === '' || typeof array[i] === 'undefined') {
      array.splice(i, 1)
      i = i - 1
    }
  }
  return array
}
function formatTime(date, format) {
  return moment(date).format(format)
}
function formatText(text, maxLength) {
  if (text) {
    if (text.length <= maxLength) {
      return text
    } else {
      return text.substring(0, maxLength)
    }
  } else {
    return text
  }
}
function test(text) {
  console.log(text)
  return 'xxxxxxx'
}

module.exports = {
  getCurrentTime: getCurrentTime,
  objLength: objLength,
  displayProp: displayProp,
  vailPhone: vailPhone,
  ifSpanish: ifSpanish,
  div: div,
  mul: mul,
  accAdd: accAdd,
  convertStarArray: convertStarArray,
  findFieldEq: findFieldEq,
  millisecondToDate: millisecondToDate,
  trimSpace: trimSpace,
  test: test,
  formatTime: formatTime,
  formatText: formatText
}
